using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using OpenComposite.EII.XSDFormGenerator;
using OpenComposite.Repository.XSDFormGenerator;

namespace OpenComposite.EII.XSDFormGenerator
{
	public partial class FormEdit : Form
	{
		private FormStyle.ItemRow _oldRow;
		private FormStyle.ItemRow _newRow;
		private string _xmlDocument = "";

		public FormEdit(string xmlDocument, FormStyle.ItemRow row)
		{
			InitializeComponent();

			_oldRow = row;
			_newRow = row;

			_xmlDocument = xmlDocument;

			Init(_oldRow);
		}

		public FormStyle.ItemRow Row
		{
			get
			{
				_newRow.Text = tbAlternativText.Text;

				_newRow.Color = btnColor.BackColor.ToArgb();

				_newRow.Font = cbFont.Text;

				_newRow.FontSize = Convert.ToDouble(cbSize.Text);

				_newRow.FontStyle = cbStyle.Text;

				_newRow.TextVisible = cbVisible.Checked;

				_newRow.Label = cbLabel.Checked;

				if ( rbLeft.Checked )
					_newRow.LabelPosition = "MiddleLeft";

				if ( rbMiddle.Checked )
					_newRow.LabelPosition = "MiddleCenter";

				if ( rbRight.Checked )
					_newRow.LabelPosition = "MiddleRight";

				if ( rbRecordView.Checked )
					_newRow.Representation = "RecordView";

				if ( rbGridView.Checked )
					_newRow.Representation = "DataGridView";

				return _newRow;
			}
		}

		private void Init(FormStyle.ItemRow row)
		{
			if ( !row.IsNameNull() )
				lblName.Text = row.Name;

			if ( !row.IsTextNull() )
				tbAlternativText.Text = row.Text;

			if ( !row.IsColorNull() )
				btnColor.BackColor = Color.FromArgb(row.Color);

			foreach ( FontFamily ff in FontFamily.Families )
				cbFont.Items.Add(ff.Name);

			if ( !row.IsFontNull() ) {
				cbFont.SelectedItem = row.Font;
			}

			if ( !row.IsFontSizeNull() ) {
				cbSize.Text = row.FontSize.ToString();
			}

			if ( !row.IsFontStyleNull() ) {
				cbStyle.SelectedItem = row.FontStyle;
			}

			if ( !row.IsTextVisibleNull() )
				cbVisible.Checked = row.TextVisible;

			if ( !row.IsLabelNull() )
				cbLabel.Checked = row.Label;

			if ( !row.IsLabelPositionNull() ) {
				switch ( row.LabelPosition ) {
					case "TopLeft":
						rbLeft.Checked = true;
						break;
					case "TopCenter":
						rbMiddle.Checked = true;
						break;
					case "TopRight":
						rbRight.Checked = true;
						break;

					case "MiddleLeft":
						rbLeft.Checked = true;
						break;
					case "MiddleCenter":
						rbMiddle.Checked = true;
						break;
					case "MiddleRight":
						rbRight.Checked = true;
						break;

					case "BottomLeft":
						rbLeft.Checked = true;
						break;
					case "BottomCenter":
						rbMiddle.Checked = true;
						break;
					case "BottomRight":
						rbRight.Checked = true;
						break;
					default:
						rbLeft.Checked = true;
						break;
				}
			}

			if ( !row.IsMultiRepresentationNull() ) {
				if ( XSDFormGenerator.MultiRepresentation(_xmlDocument, row.Name) )//if (row.MultiRepresentation)
                {
					switch ( row.Representation ) {
						case "DataGridView":
							rbGridView.Checked = true;
							break;
						case "RecordView":
							rbRecordView.Checked = true;
							break;
						default:
							rbGridView.Checked = true;
							break;
					}
				} else {
					rbGridView.Enabled = false;
					rbRecordView.Enabled = false;
				}
			}

		}

		private void textBox2_TextChanged(object sender, EventArgs e)
		{

		}

		private void FormEdit_Load(object sender, EventArgs e)
		{
			cbVisible_CheckedChanged(cbVisible, new EventArgs());
			cbLabel_CheckedChanged(cbLabel, new EventArgs());
		}

		private void tbColor_TextChanged(object sender, EventArgs e)
		{

		}

		private void btnColor_Click(object sender, EventArgs e)
		{
			colorDialog1.Color = btnColor.BackColor;
			DialogResult res = colorDialog1.ShowDialog();
			if ( res == DialogResult.OK )
				btnColor.BackColor = colorDialog1.Color;
		}

		private void comboBox2_SelectedIndexChanged(object sender, EventArgs e)
		{

		}

		private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
		{

		}

		private void cbVisible_CheckedChanged(object sender, EventArgs e)
		{
			CheckBox cb = sender as CheckBox;
			if ( cb.Checked ) {
				cbLabel.Enabled = true;
				btnColor.Enabled = true;
			} else {
				cbLabel.Enabled = false;
				btnColor.Enabled = false;
			}
		}

		private void cbLabel_CheckedChanged(object sender, EventArgs e)
		{
			CheckBox cb = sender as CheckBox;
			if ( cb.Checked ) {
				rbLeft.Enabled = true;
				rbMiddle.Enabled = true;
				rbRight.Enabled = true;
			} else {
				rbLeft.Enabled = false;
				rbMiddle.Enabled = false;
				rbRight.Enabled = false;
			}
		}

		private void button1_Click(object sender, EventArgs e)
		{
			this.DialogResult = DialogResult.OK;
			this.Close();
		}

		private void button2_Click(object sender, EventArgs e)
		{
			this.DialogResult = DialogResult.Cancel;
			this.Close();
		}

	}
}